package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Group;
import com.hcrazy.manchester.app.pojo.PageModel;

public class GroupDAO extends BaseDAO<Group>{

	public void addGroup(Group group) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into him_forum_group(");
		sb.append("groupId,joinType,viewPerm,dateline,founderUid,founderName,banner,description_mc,praiseBaseCount,praiseCount,commentBaseCount,commentCount,courseNum");
		sb.append(") values (");
		sb.append(":groupId,:joinType,:viewPerm,:dateline,:founderUid,:founderName,:banner,:description_mc,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount,:courseNum");
		sb.append(")");
		
		update(sb.toString(), group);
	}
	
	public void updateGroup(Group group) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("update him_forum_group set ");
		sb.append(" joinType=:joinType,viewPerm=:viewPerm,lastupdate=:lastupdate,banner=:banner,description_mc=:description_mc,");
		sb.append(" praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount,commentCount=:commentCount,courseNum=:courseNum");
		sb.append(" where groupId = :groupId");
		
		update(sb.toString(), group);
	}
	
	public void delGroup(int groupId) throws Exception{
		String sql = "delete from him_forum_group where groupId = ?";
		this.commonUpdate(sql, groupId);
	}
	
	public Group getGroupById(int groupId) throws Exception{
		String sql = "select a.*,c.`name`,c.sortId,c.`level` from him_forum_group a,him_forum_forum c where  a.groupId = c.forumId and a.groupId = ?";
		return getJavaBean(sql, Group.class, groupId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.sortName,c.`name`,c.sortId,c.`level` from him_forum_group a,him_live_sort b,him_forum_forum c where c.sortId = b.sortId and a.groupId = c.forumId) as d");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.sortName,c.`name`,c.sortId,c.`level` from him_forum_group a,him_live_sort b,him_forum_forum c where c.sortId = b.sortId and a.groupId = c.forumId) as d ");
		StringBuilder whereSQL = new StringBuilder(" where name like ?  ");
		List<Object> paramList = new ArrayList<Object>();
		if(null == search){
			search = "";
		}
		paramList.add("%"+search+"%");
		if(sortId > 0){
			whereSQL.append(" and sortId = ?");
			paramList.add(sortId);
		}
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Group.class);
	}
	
	public long getPraiseCount(long infoId) throws Exception{
		String sql = "select praiseCount from him_forum_group where groupId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getPraiseBaseCount(long infoId) throws Exception{
		String sql = "select praiseBaseCount from him_forum_group where groupId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getCommentCount(long infoId) throws Exception{
		String sql = "select commentCount from him_forum_group where groupId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public long getCommentBaseCount(long infoId) throws Exception{
		String sql = "select commentBaseCount from him_forum_group where groupId = ?";
		return this.getJdbcTemplate().queryForLong(sql,infoId);
	}
	
	public void addPraiseCount(long infoId) throws Exception {
		String sql = "update him_forum_group set praiseCount = praiseCount + 1 where groupId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public void addCommentCount(long infoId) throws Exception {
		String sql = "update him_forum_group set commentCount = commentCount + 1 where groupId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	
}
